

<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
  <meta charset="utf-8" />
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  
  <title>mindspore.nn.Pad &mdash; MindSpore master documentation</title>
  

  
  <link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />

  
  

  
  

  

  
  <!--[if lt IE 9]>
    <script src="../../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
        <script src="../../_static/jquery.js"></script>
        <script src="../../_static/underscore.js"></script>
        <script src="../../_static/doctools.js"></script>
        <script src="../../_static/language_data.js"></script>
        <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="../../_static/js/theme.js"></script>

    
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" />
    <link rel="next" title="mindspore.nn.Range" href="mindspore.nn.Range.html" />
    <link rel="prev" title="mindspore.nn.OneHot" href="mindspore.nn.OneHot.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../index.html" class="icon icon-home"> MindSpore
          

          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        
        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">MindSpore Python API</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../mindspore.html">mindspore</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.common.initializer.html">mindspore.common.initializer</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.communication.html">mindspore.communication</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.compression.html">mindspore.compression</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.context.html">mindspore.context</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.dataset.html">mindspore.dataset</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.dataset.audio.html">mindspore.dataset.audio</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.dataset.config.html">mindspore.dataset.config</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.dataset.text.html">mindspore.dataset.text</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.dataset.transforms.html">mindspore.dataset.transforms</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.dataset.vision.html">mindspore.dataset.vision</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.mindrecord.html">mindspore.mindrecord</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="../mindspore.nn.html">mindspore.nn</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="../mindspore.nn.html#id1">基本构成单元</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.nn.html#id2">容器</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.nn.html#id3">卷积层</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.nn.html#id4">梯度</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.nn.html#id5">循环层</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.nn.html#id6">稀疏层</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.nn.html#id7">非线性激活函数</a></li>
<li class="toctree-l2 current"><a class="reference internal" href="../mindspore.nn.html#id8">工具</a><ul class="current">
<li class="toctree-l3"><a class="reference internal" href="mindspore.nn.ClipByNorm.html">mindspore.nn.ClipByNorm</a></li>
<li class="toctree-l3"><a class="reference internal" href="mindspore.nn.Dense.html">mindspore.nn.Dense</a></li>
<li class="toctree-l3"><a class="reference internal" href="mindspore.nn.Dropout.html">mindspore.nn.Dropout</a></li>
<li class="toctree-l3"><a class="reference internal" href="mindspore.nn.Flatten.html">mindspore.nn.Flatten</a></li>
<li class="toctree-l3"><a class="reference internal" href="mindspore.nn.L1Regularizer.html">mindspore.nn.L1Regularizer</a></li>
<li class="toctree-l3"><a class="reference internal" href="mindspore.nn.Norm.html">mindspore.nn.Norm</a></li>
<li class="toctree-l3"><a class="reference internal" href="mindspore.nn.OneHot.html">mindspore.nn.OneHot</a></li>
<li class="toctree-l3 current"><a class="current reference internal" href="#">mindspore.nn.Pad</a></li>
<li class="toctree-l3"><a class="reference internal" href="mindspore.nn.Range.html">mindspore.nn.Range</a></li>
<li class="toctree-l3"><a class="reference internal" href="mindspore.nn.ResizeBilinear.html">mindspore.nn.ResizeBilinear</a></li>
<li class="toctree-l3"><a class="reference internal" href="mindspore.nn.Roll.html">mindspore.nn.Roll</a></li>
<li class="toctree-l3"><a class="reference internal" href="mindspore.nn.Tril.html">mindspore.nn.Tril</a></li>
<li class="toctree-l3"><a class="reference internal" href="mindspore.nn.Triu.html">mindspore.nn.Triu</a></li>
<li class="toctree-l3"><a class="reference internal" href="mindspore.nn.Unfold.html">mindspore.nn.Unfold</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.nn.html#id9">图像</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.nn.html#id10">归一化层</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.nn.html#id11">池化层</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.nn.html#id12">量化</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.nn.html#id13">损失函数</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.nn.html#id14">优化器</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.nn.html#wrapper">Wrapper</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.nn.html#id15">数学运算</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.nn.html#id16">评估指标</a></li>
<li class="toctree-l2"><a class="reference internal" href="../mindspore.nn.html#id17">动态学习率</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.nn.probability.html">mindspore.nn.probability</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.nn.transformer.html">mindspore.nn.transformer</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.numpy.html">mindspore.numpy</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.ops.html">mindspore.ops</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.parallel.html">mindspore.parallel</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.parallel.nn.html">mindspore.parallel.nn</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.profiler.html">mindspore.profiler</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.scipy.html">mindspore.scipy</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.train.html">mindspore.train</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.boost.html">mindspore.boost</a></li>
</ul>
<p class="caption"><span class="caption-text">MindSpore C++ API</span></p>
<ul>
<li class="toctree-l1"><a class="reference external" href="https://www.mindspore.cn/lite/api/zh-CN/master/api_cpp/mindspore.html">MindSpore Lite↗</a></li>
</ul>

            
          
        </div>
        
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../index.html">MindSpore</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          

















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../index.html" class="icon icon-home"></a> &raquo;</li>
        
          <li><a href="../mindspore.nn.html">mindspore.nn</a> &raquo;</li>
        
      <li>mindspore.nn.Pad</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
          
            <a href="../../_sources/api_python/nn/mindspore.nn.Pad.rst.txt" rel="nofollow"> View page source</a>
          
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <div class="section" id="mindspore-nn-pad">
<h1>mindspore.nn.Pad<a class="headerlink" href="#mindspore-nn-pad" title="Permalink to this headline">¶</a></h1>
<dl class="class">
<dt id="mindspore.nn.Pad">
<em class="property">class </em><code class="sig-prename descclassname">mindspore.nn.</code><code class="sig-name descname">Pad</code><span class="sig-paren">(</span><em class="sig-param">paddings</em>, <em class="sig-param">mode='CONSTANT'</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.nn.Pad" title="Permalink to this definition">¶</a></dt>
<dd><p>根据 <cite>paddings</cite> 和 <cite>mode</cite> 对输入进行填充。</p>
<p><strong>参数：</strong></p>
<ul>
<li><p><strong>paddings</strong> (tuple) - 填充大小，其shape为(N, 2)，N是输入数据的维度，填充的元素为int类型。对于 <cite>x</cite> 的第 <cite>D</cite> 个维度，paddings[D, 0]表示要在输入Tensor的第 <cite>D</cite> 个维度之前扩展的大小，paddings[D, 1]表示在输入Tensor的第 <cite>D</cite> 个维度后面要扩展的大小。输出的每个维度D的填充大小为： <span class="math notranslate nohighlight">\(paddings[D, 0] + input\_x.dim\_size(D) + paddings[D, 1]\)</span></p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># 假设参数和输入如下：</span>
<span class="n">mode</span> <span class="o">=</span> <span class="s2">&quot;CONSTANT&quot;</span><span class="o">.</span>
<span class="n">paddings</span> <span class="o">=</span> <span class="p">[[</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">],</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span><span class="mi">2</span><span class="p">]]</span><span class="o">.</span>
<span class="n">x</span> <span class="o">=</span> <span class="p">[[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">],</span> <span class="p">[</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">6</span><span class="p">],</span> <span class="p">[</span><span class="mi">7</span><span class="p">,</span><span class="mi">8</span><span class="p">,</span><span class="mi">9</span><span class="p">]]</span><span class="o">.</span>
<span class="c1"># `x` 的第一个维度为3， `x` 的第二个维度为3。</span>
<span class="c1"># 根据以上公式可得：</span>
<span class="c1"># 输出的第一个维度是paddings[0][0] + 3 + paddings[0][1] = 1 + 3 + 1 = 5。</span>
<span class="c1"># 输出的第二个维度是paddings[1][0] + 3 + paddings[1][1] = 2 + 3 + 2 = 7。</span>
<span class="c1"># 所以最终的输出shape为(5, 7)</span>
</pre></div>
</div>
</li>
<li><p><strong>mode</strong> (str) - 指定填充模式。取值为”CONSTANT”，”REFLECT”，”SYMMETRIC”。默认值：”CONSTANT”。</p></li>
</ul>
<p><strong>输入：</strong></p>
<ul class="simple">
<li><p><strong>x</strong> (Tensor) - 输入Tensor。</p></li>
</ul>
<p><strong>输出：</strong></p>
<p>Tensor，填充后的Tensor。</p>
<ul class="simple">
<li><p>如果 <cite>mode</cite> 为”CONSTANT”， <cite>x</cite> 使用0进行填充。例如， <cite>x</cite> 为[[1,2,3]，[4,5,6]，[7,8,9]]， <cite>paddings</cite> 为[[1,1]，[2,2]]，则输出为[[0,0,0,0,0,0,0]，[0,0,1,2,3,0,0]，[0,0,4,5,6,0,0]，[0,0,7,8,9,0,0]，[0,0,0,0,0,0,0]]。</p></li>
<li><p>如果 <cite>mode</cite> 为”REFLECT”， <cite>x</cite> 使用对称轴进行对称复制的方式进行填充（复制时不包括对称轴）。例如 <cite>x</cite> 为[[1,2,3]，[4,5,6]，[7,8,9]]， <cite>paddings</cite> 为[[1,1]，[2,2]]，则输出为[[6,5,4,5,6,5,4]，[3,2,1,2,3,2,1]，[6,5,4,5,6,5,4]，[9,8,7,8,9,8,7]，[6,5,4,5,6,5,4]]。</p></li>
<li><p>如果 <cite>mode</cite> 为”SYMMETRIC”，此填充方法类似于”REFLECT”。也是根据对称轴填充，包含对称轴。例如 <cite>x</cite> 为[[1,2,3]，[4,5,6]，[7,8,9]]， <cite>paddings</cite> 为[[1,1]，[2,2]]，则输出为[[2,1,1,2,3,3,2]，[2,1,1,2,3,3,2]，[5,4,4,5,6,6,5]，[8,7,7,8,9,9,8]，[8,7,7,8,9,9,8]]。</p></li>
</ul>
<p><strong>异常：</strong></p>
<ul class="simple">
<li><p><strong>TypeError</strong> - <cite>paddings</cite> 不是tuple。</p></li>
<li><p><strong>ValueError</strong> - <cite>paddings</cite> 的长度超过4或其shape不是(n, 2)。</p></li>
<li><p><strong>ValueError</strong> - <cite>mode</cite> 不是’CONSTANT’，’REFLECT’或’SYMMETRIC’。</p></li>
</ul>
<p><strong>支持平台：</strong></p>
<p><code class="docutils literal notranslate"><span class="pre">Ascend</span></code> <code class="docutils literal notranslate"><span class="pre">GPU</span></code> <code class="docutils literal notranslate"><span class="pre">CPU</span></code></p>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">mindspore</span> <span class="kn">import</span> <span class="n">Tensor</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">mindspore.nn</span> <span class="k">as</span> <span class="nn">nn</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># If `mode` is &quot;CONSTANT&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">Net</span><span class="p">(</span><span class="n">nn</span><span class="o">.</span><span class="n">Cell</span><span class="p">):</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="gp">... </span>        <span class="nb">super</span><span class="p">(</span><span class="n">Net</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
<span class="gp">... </span>        <span class="bp">self</span><span class="o">.</span><span class="n">pad</span> <span class="o">=</span> <span class="n">nn</span><span class="o">.</span><span class="n">Pad</span><span class="p">(</span><span class="n">paddings</span><span class="o">=</span><span class="p">((</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">)),</span> <span class="n">mode</span><span class="o">=</span><span class="s2">&quot;CONSTANT&quot;</span><span class="p">)</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">construct</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>
<span class="gp">... </span>        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">pad</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="p">[</span><span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">]]),</span> <span class="n">mindspore</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">pad</span> <span class="o">=</span> <span class="n">Net</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">output</span> <span class="o">=</span> <span class="n">pad</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">output</span><span class="p">)</span>
<span class="go">[[0. 0. 0. 0. 0. 0. 0.]</span>
<span class="go">[0. 0. 1. 2. 3. 0. 0.]</span>
<span class="go">[0. 0. 4. 5. 6. 0. 0.]</span>
<span class="go">[0. 0. 0. 0. 0. 0. 0.]]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># Another way to call</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">pad</span> <span class="o">=</span> <span class="n">ops</span><span class="o">.</span><span class="n">Pad</span><span class="p">(</span><span class="n">paddings</span><span class="o">=</span><span class="p">((</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">)))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># From the above code, we can see following:</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># &quot;paddings=((1, 1), (2, 2))&quot;,</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># paddings[0][0] = 1, indicates a row of values is filled top of the input data in the 1st dimension.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># Shown as follows:</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># [[0. 0. 0.]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1">#  [1. 2. 3.]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1">#  [4. 5. 6.]]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># paddings[0][1] = 1 indicates a row of values is filled below input data in the 1st dimension.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># Shown as follows:</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># [[0. 0. 0.]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1">#  [1. 2. 3.]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1">#  [4. 5. 6.]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1">#  [0. 0. 0.]]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># paddings[1][0] = 2, indicates 2 rows of values is filled in front of input data in the 2nd dimension.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># Shown as follows:</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># [[0. 0. 0. 0. 0.]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1">#  [0. 0. 1. 2. 3.]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1">#  [0. 0. 4. 5. 6.]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1">#  [0. 0. 0. 0. 0.]]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># paddings[1][1] = 2, indicates 2 rows of values is filled in front of input data in the 2nd dimension.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># Shown as follows:</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># [[0. 0. 0. 0. 0. 0. 0.]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1">#  [0. 0. 1. 2. 3. 0. 0.]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1">#  [0. 0. 4. 5. 6. 0. 0.]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1">#  [0. 0. 0. 0. 0. 0. 0.]]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">output</span> <span class="o">=</span> <span class="n">pad</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">output</span><span class="p">)</span>
<span class="go">[[0. 0. 0. 0. 0. 0. 0.]</span>
<span class="go">[0. 0. 1. 2. 3. 0. 0.]</span>
<span class="go">[0. 0. 4. 5. 6. 0. 0.]</span>
<span class="go">[0. 0. 0. 0. 0. 0. 0.]]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># if mode is &quot;REFLECT&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">Net</span><span class="p">(</span><span class="n">nn</span><span class="o">.</span><span class="n">Cell</span><span class="p">):</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="gp">... </span>        <span class="nb">super</span><span class="p">(</span><span class="n">Net</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
<span class="gp">... </span>        <span class="bp">self</span><span class="o">.</span><span class="n">pad</span> <span class="o">=</span> <span class="n">nn</span><span class="o">.</span><span class="n">Pad</span><span class="p">(</span><span class="n">paddings</span><span class="o">=</span><span class="p">((</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">)),</span> <span class="n">mode</span><span class="o">=</span><span class="s2">&quot;REFLECT&quot;</span><span class="p">)</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">construct</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>
<span class="gp">... </span>        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">pad</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="p">[</span><span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">]]),</span> <span class="n">mindspore</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">pad</span> <span class="o">=</span> <span class="n">Net</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">output</span> <span class="o">=</span> <span class="n">pad</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">output</span><span class="p">)</span>
<span class="go">[[6. 5. 4. 5. 6. 5. 4.]</span>
<span class="go">[3. 2. 1. 2. 3. 2. 1.]</span>
<span class="go">[6. 5. 4. 5. 6. 5. 4.]</span>
<span class="go">[3. 2. 1. 2. 3. 2. 1.]]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># if mode is &quot;SYMMETRIC&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">Net</span><span class="p">(</span><span class="n">nn</span><span class="o">.</span><span class="n">Cell</span><span class="p">):</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="gp">... </span>        <span class="nb">super</span><span class="p">(</span><span class="n">Net</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
<span class="gp">... </span>        <span class="bp">self</span><span class="o">.</span><span class="n">pad</span> <span class="o">=</span> <span class="n">nn</span><span class="o">.</span><span class="n">Pad</span><span class="p">(</span><span class="n">paddings</span><span class="o">=</span><span class="p">((</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">)),</span> <span class="n">mode</span><span class="o">=</span><span class="s2">&quot;SYMMETRIC&quot;</span><span class="p">)</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">construct</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>
<span class="gp">... </span>        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">pad</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="p">[</span><span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">]]),</span> <span class="n">mindspore</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">pad</span> <span class="o">=</span> <span class="n">Net</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">output</span> <span class="o">=</span> <span class="n">pad</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">output</span><span class="p">)</span>
<span class="go">[[2. 1. 1. 2. 3. 3. 2.]</span>
<span class="go">[2. 1. 1. 2. 3. 3. 2.]</span>
<span class="go">[5. 4. 4. 5. 6. 6. 5.]</span>
<span class="go">[5. 4. 4. 5. 6. 6. 5.]]</span>
</pre></div>
</div>
</dd></dl>

</div>


           </div>
           
          </div>
          <footer>
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
        <a href="mindspore.nn.Range.html" class="btn btn-neutral float-right" title="mindspore.nn.Range" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
        <a href="mindspore.nn.OneHot.html" class="btn btn-neutral float-left" title="mindspore.nn.OneHot" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
    </div>

  <hr/>

  <div role="contentinfo">
    <p>
        &#169; Copyright 2021, MindSpore.

    </p>
  </div>
    
    
    
    Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    
    provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>
        </div>
      </div>

    </section>

  </div>
  

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>